package com.example.gok_mall.reponseData;

import com.example.gok_mall.util.PropertiesUtil;
//import org.apache.logging.log4j.util.PropertiesUtil;
import redis.clients.jedis.*;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Chakid
 * @since 2018-11-11 21:26
 */
public class RedisShardePool {

    private static ShardedJedisPool pool; //Sharded Jedis连接池

    //redis所在的IP
    private static String redisIp1= PropertiesUtil.getProperty("redis1.ip");

    //redis密码
    private static String redisPassword1=PropertiesUtil.getProperty("redis1.password");

    //redis开放的端口
    private static Integer redisPort1 =Integer.parseInt(PropertiesUtil.getProperty("redis1.port"));



    //redis所在的IP
    private static String redisIp2= PropertiesUtil.getProperty("redis2.ip");

    //redis密码
    private static String redisPassword2=PropertiesUtil.getProperty("redis2.password");

    //redis开放的端口
    private static Integer redisPort2 =Integer.parseInt(PropertiesUtil.getProperty("redis2.port"));

    //jedis最大连接数
    private static Integer maxTotal= Integer.parseInt(PropertiesUtil.getProperty("redis.max.total","20"));

    //最大空闲连接数
    private static Integer maxIdle= Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle","10"));

    //最小空闲连接数
    private static Integer minIdle= Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle","2"));

    //从jedis连接池获取连接时，校验并返回可用的连接
    private static boolean testBorrow= Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow","true"));

    //把连接放回jedis连接池时，校验并返回可用的连接
    private static boolean testReturn= Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return","true"));


    //初始化连接池
    private static void initPool(){

        JedisPoolConfig config = new JedisPoolConfig();

        //给config set相关值
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testBorrow);
        config.setTestOnReturn(testReturn);


        //连接耗尽时，是否阻塞，false会抛出异常，true直到阻塞超时，默认为true
        config.setBlockWhenExhausted(true);

        JedisShardInfo info1 = new JedisShardInfo(redisIp1,redisPort1,1000*2);
        info1.setPassword(redisPassword1);


        JedisShardInfo info2 = new JedisShardInfo(redisIp2,redisPort2,1000*2);
        info2.setPassword(redisPassword2);

        //后面的2标志几个元素  不写也可以就使用默认长度
        List<JedisShardInfo> jedisShardInfoList = new ArrayList<JedisShardInfo>(2);

        jedisShardInfoList.add(info1);
        jedisShardInfoList.add(info2);

        pool = new ShardedJedisPool(config,jedisShardInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);


    }


    //实例化连接池
    static {
        initPool();
    }

    //从连接池中获取一个连接对象
    public static ShardedJedis getJedis(){
        return pool.getResource();
    }


    //如果是一个损坏连接就放在BrokenResource
    public static void returnBrokenResource(ShardedJedis jedis){
        pool.returnBrokenResource(jedis);
    }

    //将jedis放回连接池
    public static void returnResource(ShardedJedis jedis){
        pool.returnResource(jedis);
    }



    //用main测试我们的连接

    public static void main(String[] args) {
        ShardedJedis jedis = pool.getResource();

        for(int i=0; i<10; i++){
            jedis.set("key"+i,"value"+i);
        }

        returnResource(jedis);

  //      pool.destroy();

        System.out.println("program is end~");
    }
}
